/*
 *
 *    BLITZ Google Analytics for iPhone
 *    Copyright (C) 2009 BLITZ Digital Studios LLC d/b/a BLITZ Agency.
 *
 *    This library is free software; you can redistribute it and/or modify it 
 *    under the terms of the GNU Lesser General Public License as published
 *    by the Free Software Foundation; either version 2.1 of the License, or 
 *    (at your option) any later version.
 *
 *    This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 *    without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
 *    PURPOSE. See the GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License along 
 *    with this library; if not, write to the Free Software Foundation, Inc.,
 *    59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 *
 *    BLTIZ Digital Studios LLC, 3415 S. Sepulveda BLVD, Ste 500, Los Angeles CA, 90034
 *    http://www.blitzagency.com
 *    http://labs.blitzagency.com
 *
 *        Author: Adam Venturella - aventurella@blitzagency.com
 *
 */

#import "GoogleAnalyticsObject.h"
#import <sqlite3.h>
static sqlite3_stmt * insert_statement  = nil;
static sqlite3_stmt * delete_statement  = nil;

@implementation GoogleAnalyticsObject
@synthesize objectid;
-(void) dealloc
{
	[value release];
	[super dealloc];
}

-(id)initWithPrimaryKey:(int)anId
{
	if(self = [super init])
	{
		objectid = anId;
	}
	
	return self;
}

+(void)finalizeStatements
{
	sqlite3_finalize(insert_statement);
	sqlite3_finalize(delete_statement);
}

-(void)removeFromDatabase:(sqlite3 *)database
{
	assert(database != nil);
	
	if (delete_statement == nil) 
	{
        static char *sql = "DELETE FROM analyticsobject WHERE objectid = ?";
        
		if (sqlite3_prepare_v2(database, sql, -1, &delete_statement, NULL) != SQLITE_OK) 
		{
            NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
        }
    }
	
	sqlite3_bind_int(delete_statement, 1, self.objectid);
	int success = sqlite3_step(delete_statement);
	sqlite3_reset(delete_statement);
	
	if (success != SQLITE_DONE) {
        NSAssert1(0, @"Error: failed to delete from database with message '%s'.", sqlite3_errmsg(database));
    }
}

-(void)insertIntoDatabase:(sqlite3 *)database
{
	assert(database != nil);
	
	if (insert_statement == nil) 
	{
        static char *sql = "INSERT INTO analyticsobject (value) VALUES(?)";
        
		if (sqlite3_prepare_v2(database, sql, -1, &insert_statement, NULL) != SQLITE_OK) 
		{
            NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
        }
    }
    
	sqlite3_bind_text(insert_statement, 1, [value UTF8String], -1, SQLITE_TRANSIENT);
	
	int success = sqlite3_step(insert_statement);
	
	// Because we want to reuse the statement, we "reset" it instead of "finalizing" it.
    sqlite3_reset(insert_statement);
	
	if (success != SQLITE_DONE) {
        NSAssert1(0, @"Error: failed to insert from database with message '%s'.", sqlite3_errmsg(database));
    }
}

#pragma mark Accessors
- (NSString *)value {
    return value;
}

- (void)setValue:(NSString *)aString {
    
	if ((!value && !aString) || (value && aString && [value isEqualToString:aString])) return;
    
	dirty = YES;
    [value release];
    value = [aString copy];
}

@end
